package com.zc.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.zc.dao.IFaultDao;
import com.zc.domain.DoubleListData;
import com.zc.domain.FaultItem;
import com.zc.domain.FaultStatisticsItem;
import com.zc.domain.FourPointsItem;
import com.zc.domain.Item;
import com.zc.domain.TwoPointsItem;
import com.zc.service.FaultService;
import com.zc.service.RedisService;

@Controller
public class FaultController {
	@Autowired
	private FaultService faultService;
	@Autowired
	private RedisService redisService;

	@RequestMapping(value = "/getCurFault", method = RequestMethod.POST)
	public @ResponseBody List<FaultItem> getCurFault() {
		String key = "curFaultData";
		List<FaultItem> curFaultData = null;
		if (redisService.hasKey(key)) {
			curFaultData = (List<FaultItem>) redisService.get(key);
		} else {
			curFaultData = faultService.getCurFault();
			redisService.set(key, curFaultData);
		}
		return curFaultData;
	}

	@RequestMapping(value = "/getAllFault", method = RequestMethod.POST)
	public @ResponseBody List<FaultItem> getAllFault(@RequestParam("flag") String flag) {
		String channel = null;
		if (flag.equals("1"))
			channel = "主提故障";
		else if (flag.equals("0"))
			channel = "副提故障";
		else
			return null;
		System.out.println(channel);
		return faultService.getAllFault(channel);
	}

	@RequestMapping(value = "/getTodayFaultStat", method = RequestMethod.POST)
	public @ResponseBody List<FaultStatisticsItem> getTodayFaultStat(@RequestParam("flag") String flag) {
		String channel = null;
		if (flag.equals("1"))
			channel = "主提故障";
		else if (flag.equals("0"))
			channel = "副提故障";
		else
			return null;
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");// 设置日期格式
		String date = df.format(new Date());// new Date()为获取当前系统时间
		List<FaultStatisticsItem> res = faultService.getCurFaultStat(channel, date);
		return res;
	}

	@RequestMapping(value = "/getThisMonthFaultStat", method = RequestMethod.POST)
	public @ResponseBody List<FaultStatisticsItem> getThisMonthFaultStat(@RequestParam("flag") String flag) {
		String channel = null;
		if (flag.equals("1"))
			channel = "主提故障";
		else if (flag.equals("0"))
			channel = "副提故障";
		else
			return null;
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM");// 设置日期格式
		String date = df.format(new Date());// new Date()为获取当前系统时间
		return faultService.getCurFaultStat(channel, date);
	}

	@RequestMapping(value = "/fault/daySelect", method = RequestMethod.POST)
	public @ResponseBody List<FaultStatisticsItem> daySelect(@RequestParam("flag") String flag,
			@RequestParam("beginDate") String beginDate) {
		String channel = null;
		if (flag.equals("1"))
			channel = "主提故障";
		else if (flag.equals("0"))
			channel = "副提故障";
		else
			return null;

		return faultService.daySelect(channel, beginDate);
	}

	@RequestMapping(value = "/fault/monthSelect", method = RequestMethod.POST)
	public @ResponseBody List<FaultStatisticsItem> monthSelect(@RequestParam("flag") String flag,
			@RequestParam("beginDate") String beginDate) {
		String channel = null;
		if (flag.equals("1"))
			channel = "主提故障";
		else if (flag.equals("0"))
			channel = "副提故障";
		else
			return null;

		return faultService.monthSelect(channel, beginDate);
	}
}
